/**
 * @(#) popMedicalSubtractionMediator.as
 *
 * @date		2009. 04. 20.
 * @version		1.0
 * @author		Jo, ChangHun
 * @description	연말정산 메인 view - Mediator
 * 				연말정산 - 탭6 의료비 공제 팝업창 Logic을 관리 
 */
 package com.skt.common_hr.yearadjustment.view
 {
 	import com.skt.common_hr.common.PsHrAlert;
 	import com.skt.common_hr.common.Variables;
 	import com.skt.common_hr.common.puremvc.CommonMediator;
 	import com.skt.common_hr.yearadjustment.YearAdjustmentFacade;
 	import com.skt.common_hr.yearadjustment.model.AnnuitySubtractionProxy;
 	import com.skt.common_hr.yearadjustment.model.BankListProxy;
 	import com.skt.common_hr.yearadjustment.model.BizNumValidProxy;
 	import com.skt.common_hr.yearadjustment.model.vo.AnnuitySubtractionVO;
 	import com.skt.common_hr.yearadjustment.view.components.popAnnuitySubtraction;
 	import com.skt.common_hr.yearadjustment.view.components.popMedicalSubtraction;
 	
 	import flash.events.Event;
 	import flash.events.MouseEvent;
 	
 	import mx.collections.ArrayCollection;
 	import mx.managers.PopUpManager;
 	
 	import org.puremvc.as3.multicore.interfaces.INotification;
 	
 	
 	public class popAnnuitySubtractionMediator extends CommonMediator
 	{
 		public static const NAME:String = "popAnnuitySubtractionMediator";
 		public var v_year:String = "";
		[Bindable] public var v_enable:Boolean = true;
//shgoo 2010.01.12
		//추가, 수정 구분
		private var bAdd:Boolean = true;
				 		
 		/**
 		 * 생성자
 		 * 
 		 * @param
 		 * @return void
 		 * */
 		public function popAnnuitySubtractionMediator( viewComponent:Object ):void
 		{
 			super(NAME, viewComponent);
 			view.callLater(initView);
 		}
 		
 		/**
 		 * View를 초기화 한다.
 		 * 	- View에 기본사항을 출력하고 data를 load
 		 * @param
 		 * @return void
 		 * */
 		public function initView():void
 		{
 			view.addEventListener(popMedicalSubtraction.OkInput, onClickSaveAnnuitySubtraction);
 			view.addEventListener(popMedicalSubtraction.CancelInput, onCancelInput);
//shgoo 2010.01.11
 			//view.btnAdd.addEventListener(MouseEvent.CLICK, onAdd);
 			//view.btnModify.addEventListener(MouseEvent.CLICK, onModify);
/////////////////

 			view._isEnable = v_enable;
 			loadAnnuitySubtractionData();
 			loadBankList();
 			//comboJumin();
 		}
 		
//shgoo 2010.01.11 		
 		//추가버튼 클릭 이벤트
 		private function onAdd(event:Event):void {
			bAdd = true;
			/*
 			if(view.RADIO.selectedValue == "X") {	//국세청 자료 총합으로 입력 인경우
				//항목 추가
				view.addArray();
 			}
 			else {	//국세청 자료 총합 입력이 아닌경우
 				//사업자  번호 유효성 체크
 				var proxy:BizNumValidProxy = facade.retrieveProxy(BizNumValidProxy.NAME) as BizNumValidProxy;
 				proxy.load(view.D_COMCD.text);
 			}
 			*/
 		}
 		
 		//수정버튼 클릭 이벤트
 		private function onModify(event:Event):void {
			bAdd = false;
			/*
 			if(view.RADIO.selectedValue == "X") {	//국세청 자료 총합으로 입력 인경우
				//항목 수정
				view.onModifyItem();
 			}
 			else {	//국세청 자료 총합 입력이 아닌경우
 				//사업자  번호 유효성 체크
 				var proxy:BizNumValidProxy = facade.retrieveProxy(BizNumValidProxy.NAME) as BizNumValidProxy;
 				proxy.load(view.D_COMCD.text);
 			}
 			*/
 		}

///////////////////
 		
 		//연금보혐로 공제유형
 		private function loadAnnuityTypeList():void{
 			var proxy:AnnuitySubtractionProxy = facade.retrieveProxy(AnnuitySubtractionProxy.NAME) as AnnuitySubtractionProxy;
 			proxy.loadAnnuityType(v_year);
 		}
 		
 		private function loadBankList():void{
 			var proxy:BankListProxy= facade.retrieveProxy(BankListProxy.NAME) as BankListProxy;
 			proxy.loadBankList();
 		}
 		
 		//기본 데이터 조회
 		private function loadAnnuitySubtractionData():void{
 			var proxy:AnnuitySubtractionProxy = facade.retrieveProxy(AnnuitySubtractionProxy.NAME) as AnnuitySubtractionProxy;
 			proxy.load(Variables.yearPernr, v_year);
 		}
 		
 		//저장
 		private function onClickSaveAnnuitySubtraction(event:Event):void{

 			var tableArr:Array = new Array();
	 		for each(var rec:AnnuitySubtractionVO in view.annuityData)
	 		{
	 			var data:Object = new Object;
	 			data.PREIN = rec.PREIN;
	 			data.PNSTY = rec.PNSTY;
	 			data.BETRG = rec.BETRG;
	 			data.FINCO = rec.FINCO;
	 			data.INSNM = rec.INSNM;
	 			data.ACCNO = rec.ACCNO;
	 			
	 			tableArr.push(data);
	 		}
 			
 			var proxy:AnnuitySubtractionProxy = facade.retrieveProxy(AnnuitySubtractionProxy.NAME) as AnnuitySubtractionProxy;
 			proxy.save(Variables.yearPernr, v_year, tableArr);
 		}
 		
 		//팝업 닫기
 		private function onCancelInput(event:Event):void
 		{
 			facade.removeMediator(popAnnuitySubtractionMediator.NAME);
 			PopUpManager.removePopUp(view);
 		}
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					YearAdjustmentFacade.LOAD_ANNUITY_SUBTRATION_COMPLETE,
					YearAdjustmentFacade.LOAD_ANNUITY_SUBTRATION_FAILED,
					YearAdjustmentFacade.LOAD_ANNUITY_TYPE_LIST_COMPLETE,
					YearAdjustmentFacade.LOAD_ANNUITY_TYPE_LIST_FAILED,
					YearAdjustmentFacade.SAVE_ANNUITY_SUBTRATION_COMPLETE,
					YearAdjustmentFacade.SAVE_ANNUITY_SUBTRATION_FAILED,
					BankListProxy.LOAD_BANK_LIST_COMPLETE,
					BankListProxy.LOAD_BANK_LIST_FAILED
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case YearAdjustmentFacade.LOAD_ANNUITY_SUBTRATION_COMPLETE:
					view.annuityData = (note.getBody() as AnnuitySubtractionProxy).annuityData;
					loadAnnuityTypeList();
					break;
				case YearAdjustmentFacade.LOAD_ANNUITY_TYPE_LIST_COMPLETE:
					setComboDataProvider(note.getBody() as AnnuitySubtractionProxy);
					break;
				case YearAdjustmentFacade.LOAD_BIZ_NUM_VALID_COMPLETE:	//사업자 등록 번호 유효성 체크 성공
					if(bAdd)	//항목 추가
						view.addArray();
					else		//항목 수정
						view.onModifyItem();
					break;
				case YearAdjustmentFacade.LOAD_BIZ_NUM_VALID_FAILED:	//사업자 등록 번호 유효성 체크 에러
					PsHrAlert.showMessage((note.getBody() as BizNumValidProxy).msgStr);
					break;
///////////////////////			
				case BankListProxy.LOAD_BANK_LIST_COMPLETE:
					view.bankList = (note.getBody() as BankListProxy).bankList;
					break;
				case YearAdjustmentFacade.SAVE_ANNUITY_SUBTRATION_COMPLETE:
					facade.sendNotification(YearAdjustmentFacade.RELOAD_BASIC_DATA);
		 			facade.removeMediator(popAnnuitySubtractionMediator.NAME);
		 			PopUpManager.removePopUp(view);
					break;
				case YearAdjustmentFacade.SAVE_ANNUITY_SUBTRATION_FAILED:
					//PsHrAlert.showMessage((note.getBody() as BizNumValidProxy).msgStr);
					PsHrAlert.showMessage((note.getBody() as AnnuitySubtractionProxy).msgStr);
					
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
//shgoo 2010.01.07		
		//대상자 주민번호 추출
		private function getValidInhabitants(arrSrc:ArrayCollection):ArrayCollection {
			var arrDest:ArrayCollection = new ArrayCollection();
			
			//주민등록번호 선택 텍 스트 			
			arrDest.addItem(arrSrc.getItemAt(0));
			
			for each ( var rec:Object in arrSrc) {
				if(rec.DPTID == "X") {
					arrDest.addItem(rec);
				}
			}
			
			return arrDest;
		}
///////////////////
		
		private function setComboDataProvider(proxy:AnnuitySubtractionProxy):void
		{
			view.annuityTypeList = proxy.annuityTypeList;
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private function get view():popAnnuitySubtraction
		{
			return viewComponent as popAnnuitySubtraction;
		}
	}
 }